package com.springboot.base.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.base.common.*;
import com.springboot.base.model.Seller;
import com.springboot.base.request.PageQuery;
import com.springboot.base.request.SellerCreateReq;
import com.springboot.base.service.ISellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author meiguangya
 * @since 2020-07-17
 */
@Controller
@RequestMapping("/admin/seller")
public class SellerController {

    @Autowired
    private ISellerService sellerService;

    @RequestMapping("/index")
    @AdminPermission
    public ModelAndView index(PageQuery pageQuery){
        ModelAndView mv = new ModelAndView("/admin/seller/index");
        Page<Seller> pageInfo = new Page();
        pageInfo.setCurrent(pageQuery.getPage());
        pageInfo.setSize(3);
        IPage<Seller> sellerList = sellerService.qrySellerList(pageInfo);
        mv.addObject("data",sellerList);
        mv.addObject("CONTROLLER_NAME","seller");
        mv.addObject("ACTION_NAME","index");
        return mv;
    }

    @RequestMapping("/createpage")
    @AdminPermission
    public ModelAndView createPage(){
        ModelAndView modelAndView = new ModelAndView("/admin/seller/create.html");
        modelAndView.addObject("CONTROLLER_NAME","seller");
        modelAndView.addObject("ACTION_NAME","create");
        return modelAndView;
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @AdminPermission
    public String create(@Valid SellerCreateReq sellerCreateReq, BindingResult bindingResult) throws BusinessException {
        if(bindingResult.hasErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }

        Seller sellerModel = new Seller();
        sellerModel.setName(sellerCreateReq.getName());
        sellerService.create(sellerModel);

        return "redirect:/admin/seller/index";
    }

    @RequestMapping(value="down",method = RequestMethod.POST)
    @AdminPermission
    @ResponseBody
    public ServerResponse down(@RequestParam(value="id")Integer id) throws BusinessException {
        Seller sellerModel = sellerService.changeStatus(id,1);
        return ServerResponse.createBySuccessData(sellerModel);
    }

    @RequestMapping(value="up",method = RequestMethod.POST)
    @AdminPermission
    @ResponseBody
    public ServerResponse up(@RequestParam(value="id")Integer id) throws BusinessException {
        Seller sellerModel = sellerService.changeStatus(id,1);
        return ServerResponse.createBySuccessData(sellerModel);
    }

}

